package simorg.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DatabaseConnection {

	private Connection connection;
	
	public Connection getConnection() {
		return connection;
	}

	public DatabaseConnection() throws ClassNotFoundException, SQLException {
		Class.forName("org.h2.Driver");
		connection = DriverManager.getConnection("jdbc:h2:~/simorg", "sa", "");
		System.out.println("Database connection made");
	}
	
	public void close() {
		if ( this.connection != null ) {
			try {
				this.connection.close();
			}
			catch ( Exception e ) {
				throw new RuntimeException(e);
			}
		}
	}
	
	public boolean isOpen() {
		try {
			return this.connection != null && !this.connection.isClosed();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
}
